package com.hellobike.haxing.modules.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.address.entity.TCity;
import com.hellobike.haxing.modules.address.entity.TKV;
import com.hellobike.haxing.modules.address.mapper.TCityMapper;
import com.hellobike.haxing.modules.address.service.ITCityService;
import com.hellobike.haxing.modules.address.util.Convert2KV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @Description: t_city
 * @Author: haxing-boot
 * @Date:   2020-02-05
 * @Version: V1.0
 */
@Service
public class TCityServiceImpl extends ServiceImpl<TCityMapper, TCity> implements ITCityService {

    @Autowired
    private  TCityMapper tCityMapper;

    @Override
    public List<TKV> getTCitysByProvineId(Long provinceId){
        QueryWrapper<TCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("province_id", provinceId);
        return Convert2KV.city2kv(baseMapper.selectList(queryWrapper));
    }

    @Override
    public String getTCitys(List<String> cityIds){
        if(cityIds == null || cityIds.size() < 1){
            return null;
        }
        List<TCity> tCityList = baseMapper.selectBatchIds(cityIds);
        if(tCityList == null){
            return null;
        }
        List<TCity> tCities = tCityList.stream().collect(collectingAndThen(
                                     toCollection(() -> new TreeSet<>(comparing(n->n.getId()))),ArrayList::new)
                    );
        List<String> categoryStr = tCities.stream().map(TCity::getName).collect(Collectors.toList());

        return categoryStr.stream().collect(Collectors.joining(","));
    }

    @Override
    public List<TKV> getTCitys4List(List<String> cityIds){
        if(cityIds == null || cityIds.size() < 1){
            return null;
        }
        QueryWrapper<TCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("province_id",cityIds);
        List<TCity> tCityList = baseMapper.selectList(queryWrapper);
        if(tCityList == null){
            return null;
        }
        List<TCity> tCities = tCityList.stream().collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparing(n->n.getId()))),ArrayList::new)
        );
        return Convert2KV.city2kv(tCities);
    }


    @Override
    public List<TKV> getAllCitys(){
        QueryWrapper<TCity> queryWrapper = new QueryWrapper<>();
        return Convert2KV.city2kv(baseMapper.selectList(queryWrapper));
    }
}
